/*
Copyright (C) 2012 Iulian Virtejanu

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
*/

package com.nativnux.installers.iptraf;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nativnux.installer.lib.Eula;
import com.nativnux.installer.lib.InstallerActivity;
import com.nativnux.installer.lib.InstallerAsyncTask;
import com.nativnux.installer.lib.ShellExecResult;

public class Installer extends InstallerActivity {

	public static String TAG = "com.nativnux.installers.iptraf";
	
	private Button installButton;
	private ProgressBar progress;
	private TextView logTextView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		Eula.show(this, R.string.eula_title, R.string.eula_accept,
				R.string.eula_refuse);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_installer);

		InstallClickListener buttonInstallListener = new InstallClickListener(
				this);

		this.installButton = (Button) this.findViewById(R.id.buttonInstall);
		this.installButton.setOnClickListener(buttonInstallListener);

		this.progress = (ProgressBar) this
				.findViewById(R.id.progressBarInstaller);

		// Activate scrolling for text view
		this.logTextView = (TextView) this.findViewById(R.id.logTextView);
		logTextView.setMovementMethod(new ScrollingMovementMethod());

		// Detect available space
	}

	public void onPreExecute() {
		progress.setVisibility(View.VISIBLE);
		installButton.setEnabled(false);
	}

	public void onPostExecute(ShellExecResult result) {
		progress.setVisibility(View.INVISIBLE);
		installButton.setEnabled(true);
		
		if (result.getDetails() != null) {
			logTextView.setText(result.getDetails());
		}
	}
	
	class InstallClickListener implements OnClickListener {
		private InstallerActivity parent;

		InstallClickListener(InstallerActivity parent) {
			this.parent = parent;
		}

		@Override
		public void onClick(View v) {

			// Use jet extension to avoid "Data exceeds UNCOMPRESS_DATA_MAX"
			
			InstallerAsyncTask task = new InstallerAsyncTask(parent, "iptraf-3.0.0.arm.tar", 2);
			task.execute();
		}

	}
}